package com.astrocsr.common.helper;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * 
 * This class is a Singleton that provides access to systemConfig parameters
 * 
 * defined in a Properties file <i>system.properties</i>. A client gets access
 * to the single instance
 * 
 * through the static {@link ConfigHelper#getInstance() getInstance} method.
 * 
 * <p>
 * 
 * passing in the appropriate key as a parameter. Null is returned if the
 * 
 * key does not exist.
 */

public class ConfigHelper {

	private static Properties systemConfig = null;

	private static ConfigHelper instance = null;

	private final static String SYSTEM_PROPERTY_FILE = "/properties/system.properties";

	private ConfigHelper() {

		super();

	}

	/**
	 * 
	 * Returns the single instance, creating one if it's the
	 * 
	 * first time this method is called.
	 * 
	 * @return ConfigManager. The single instance.
	 */

	public static ConfigHelper getInstance() {

		// thread safe implementation - double-check idiom

		if (instance == null) {

			synchronized (ConfigHelper.class) {

				if (instance == null) {

					instance = new ConfigHelper();

					instance.loadSystemProperties();

				}

			}

		}

		return instance;

	}

	public void loadSystemProperties() {

		InputStream is = getClass().getResourceAsStream(SYSTEM_PROPERTY_FILE);

		systemConfig = new Properties();

		try {

			systemConfig.load(is);

		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	/**
	 * 
	 * Retrieves the value of a given systemConfig entry.
	 * 
	 * 
	 * 
	 * @param key
	 *            The name of an entry.
	 * 
	 * 
	 * 
	 * @return String The corresponding value. <code>null</code> is returned
	 * 
	 *         if the entry does not exist.
	 */

	public String get(String key) {

		if (systemConfig != null) {

			return systemConfig.getProperty(key);

		} else {

			return null;

		}

	}

	
	/**
	 * Checkfree: load configuration
	 */
	private static com.checkfree.isolutions.property.Properties appCfg = com.checkfree.isolutions.property.PropertiesConfig.getApplicationSettings();

	public static String getCFConfig(String key) {
		if (appCfg != null) {
			return appCfg.getProperty(key);
		} else {
			return null;
		}
	}
}
